package cn.tkb.org.controller;

import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.org.domain.DictionaryDetail;
import cn.tkb.org.query.DictionaryDetailQuery;
import cn.tkb.org.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dicts")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;
    //新增和修改
    @RequestMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId() == null){
                dictionaryDetailService.add(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }
    //查询部门
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionaryDetailService.QeuryById(id);
    }
    //删除部门
    @DeleteMapping("/{id}")
        public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }

    }
    //批量删除
    @PatchMapping
    public AjaxResult BatchDelete(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.BatchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail(e.getMessage());
        }
    }
    //查询所有部门
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.QeuryAll();
    }
    //高级查询和分页查询
    @PostMapping
    public PageQuery<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
       return dictionaryDetailService.findPage(query) ;
    }
}
